package tubesprogin4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Server {
    private ServerSocket server;
    private Socket client;
    private ObjectInputStream in = null;
    private ObjectOutputStream out = null;

    public Server(int port) throws IOException, ClassNotFoundException {
        server = new ServerSocket(port);
        //acceptConnection();
        checkLastModified("test.docx");
        sendFile();
    }
    
    private void acceptConnection() throws IOException, ClassNotFoundException {
        client = server.accept();
        initObjectStream();
        Date date = (Date) in.readObject();
        closeObjectStream();
        System.out.println(date);
    }
    
    private void initObjectStream() throws IOException {
        out = new ObjectOutputStream(client.getOutputStream());
        in = new ObjectInputStream(client.getInputStream());    
    }
    
    private void closeObjectStream() throws IOException {
        out.close();
        in.close();
    }
    
    private void checkLastModified(String fileName) {
        File file = new File(fileName);
        long lastModifiedDate = file.lastModified();
        Date date = new Date(lastModifiedDate);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        String dateString = sdf.format(date);
        System.out.println("The file was last modified on: " + dateString);
        
        /*
        int results = date.compareTo(d2);
   
        if(results>0) {
            System.out.println("First Date is after second");
        }
        else if (results<0) {
            System.out.println("First Date is before second");
        }
        else {
            System.out.println("Both dates are equal");
        }
        */
    }
    
    private void sendFile() throws IOException {
        File f = new File("test.docx");
        FileInputStream fin = new FileInputStream(f);
        byte fileContent[] = new byte[(int) f.length()];
        fin.read(fileContent);

        FileOutputStream fos = new FileOutputStream(new File("a.docx"));        
        BufferedOutputStream bos = new BufferedOutputStream(fos);        

        //split bytes
        int offset = 0;

        while (offset<fileContent.length) {
            byte[] outputBytes;

            if(fileContent.length-offset<5000) {
                outputBytes = new byte[fileContent.length-offset];
                System.arraycopy(fileContent, offset, outputBytes, 0, fileContent.length-offset);
                bos.write(outputBytes);
                bos.flush();
                break;
            }

            outputBytes = new byte[5000];
            System.arraycopy(fileContent, offset, outputBytes, 0, 5000);
            offset += 5000;     
            bos.write(outputBytes);
            bos.flush();
        }
        
        bos.close();
    }
}
